<?php
namespace common\forms\login;

use Yii;

/**
 * 验证码登录
 */
class CodeLoginForm extends BaseLoginForm
{
    public $username;
    public $password;
    public $mobile;    

    public $autoSignup = false;

    private $_user;

    const EVENT_BEFORE_LOGIN = 'beforeLogin';
    const EVENT_AFTER_LOGIN = 'afterLogin';

    /**
     * @inheritdoc
     */
    public function init(){
        $this->setScenario('username');
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'username'=>'用户名',
            'mobile'=>'手机号码',
            'email'=>'邮箱号码',
            
            'code'=>'验证码',
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['username', 'trim'],
            ['username', 'required'],
            ['username', 'string', 'min' => 2],

            ['password', 'required'],

            ['code', 'required'],
            ['code', 'validateCode'],
            ['code', 'string', 'min' => 6, 'max' => 6],
        ];
    }

    public function scenarios(){
        $scenarios = parent::scenarios();
        $scenarios['password'] = ['username', 'password'];
        $scenarios['code'] = ['mobile', 'code'];
        return $scenarios;
    }

    /**
     * 短信验证
     */
    public function validateSmsCode($attribute, $params)
    {
        //TODO 短信验证
    }

    /**
     * 会员登陆.
     *
     * @return User|null the saved model or null if saving fails
     */
    public function login($type = 'default')
    {
        if (!$this->validate()) {
            return null;
        }
        if (!$this->beforeLogin()) {
            return null;
        }

        

        switch ($this->scenario) {
            case 'smscode':
                $user = $this->smsLogin();
                break;
            
            default:
                $user = $this->passwordLogin();
                break;
        }

        if(!$user){
            return null;
        }

        return $this->vaildLogin($user);
    }

    /**
     * 短信登陆.
     *
     * @return User|null the saved model or null if saving fails
     */
    private function smsLogin()
    {
        if(!($user = $this->getUser())){
            $this->addError('username', '账号信息找不到！');
            return null;
        }
        return $user;
    }

    /**
     * 密码登陆.
     *
     * @return User|null the saved model or null if saving fails
     */
    private function passwordLogin()
    {
        if(!($user = $this->getUser())){
            $this->addError('username', '账号信息找不到！');
            return null;
        }
        if(!$user->validatePassword($this->password)){
            $this->addError('password', '密码不对！');
            return null;
        }

        return $user;
    }

    public function getUser(){
        $userClassName = Yii::$app->getUser()->identityClass;
        if(!$this->_user){
            $this->_user = $userClassName::findByUsername($this->scenario == 'username' ? $this->username : $this->mobile);
        }        
        return $this->_user;
    }

    /**
     * 商家会员登陆通过ID.
     *
     * @return User|null the saved model or null if saving fails
     */
    public function loginIdentity($id)
    {
        $userClassName = Yii::$app->getUser()->identityClass;
        $this->_user = $userClassName::findIdentity($id);
        if (!$this->beforeLogin()) {
            return false;
        }
        if(!$this->_user)return null;
        return $this->vaildLogin($this->_user);
    }

    private function vaildLogin($user){
        Yii::$app->user->login($user);
        $this->afterLogin();
        return $user;
    }
}
